package com.uex.dev.manage.admin.controller;

import com.google.common.base.Preconditions;
import com.uex.dev.manage.admin.po.DeviceExtendPO;
import com.uex.dev.manage.admin.po.DevicePO;
import com.uex.dev.manage.admin.service.IDeviceExtendService;
import com.uex.dev.manage.admin.service.IDeviceService;
import com.uex.dev.manage.admin.vo.device.*;
import com.uex.dev.manage.common.base.ModelResult;
import com.uex.dev.manage.common.base.PageResult;
import io.micrometer.common.util.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author lzs
 * @create 2024/02/22
 * @description
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Tag(name = "设备管理")
@RequestMapping("/device")
@RestController
public class DeviceController {

    @Resource
    private IDeviceService deviceService;

    @Resource
    private IDeviceExtendService deviceExtendService;

    @Operation(description = "设备唯一标识替换")
    @PostMapping("/replaceToken")
    public ModelResult<?> replaceTokenDevice(@RequestBody ReplaceTokenReq replaceTokenReq) {
        return deviceService.replaceToken(replaceTokenReq) ? ModelResult.SUCCESS_RESULT : ModelResult.SYSTEM_ERROR_RESULT;
    }

    @Operation(description = "获取设备列表")
    @PostMapping("/page")
    public ModelResult<PageResult<DevicePageResp>> pageDevice(@RequestBody DevicePageReq devicePageReq) {
        return new ModelResult<>(deviceService.pageDevice(devicePageReq));
    }

    @Operation(description = "获取项目对应设备列表")
    @GetMapping("/list")
    public ModelResult<List<DevicePageResp>> getDeviceList(@RequestParam Integer prjId) {
        return new ModelResult<>(deviceService.getDeviceList(prjId));
    }

    @Operation(description = "获取设备基本信息")
    @GetMapping("/info")
    public ModelResult<DeviceInfoResp> getInfoDevice(@RequestParam String deviceToken) {
        Preconditions.checkArgument(StringUtils.isNotBlank(deviceToken), "设备唯一标识不能为空");
        return new ModelResult<>(deviceService.getInfoDevice(deviceToken));
    }

    @Operation(description = "修改设备基本信息")
    @PostMapping("/info")
    public ModelResult<?> updateInfoDevice(@RequestBody DevicePO deviceUpdateInfoReq) {
        deviceService.updateInfoDevice(deviceUpdateInfoReq);
        return ModelResult.SUCCESS_RESULT;
    }

    @Operation(description = "获取设备生产及发货信息")
    @GetMapping("/delivery")
    public ModelResult<DeviceDeliveryResp> getDeliveryDevice(@RequestParam Integer deviceId) {
        return new ModelResult<>(deviceExtendService.getDeliveryDevice(deviceId));
    }

    @Operation(description = "更新设备生产及发货信息")
    @PostMapping("/delivery")
    public ModelResult<?> updateDeliveryDevice(@RequestBody DeviceExtendPO deviceModExtendReq) {
        deviceExtendService.updateDeliveryDevice(deviceModExtendReq);
        return ModelResult.SUCCESS_RESULT;
    }

    @Operation(description = "导出设备能力")
    @GetMapping("/capacity/export")
    public ModelResult<?> exportDevice(@RequestParam String deviceToken, HttpServletResponse response) {
        deviceService.exportDevice(deviceToken, response);
        return ModelResult.SUCCESS_RESULT;
    }

    @Operation(description = "删除设备")
    @DeleteMapping("/del")
    public ModelResult<?> delDev(Integer id){
        Preconditions.checkArgument(Objects.nonNull(id),"设备ID不能为空");
        deviceService.removeById(id);
        return ModelResult.SUCCESS_RESULT;
    }
}
